package spike.tweet;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import spike.db.QueryPool;
 
public class TweetGen {
	private String keyword = "";
	QueryPool query = new QueryPool();
	
	private HashMap<String, String> map = new HashMap();
	
	public TweetGen(String keyword) {
		this.keyword = keyword;
	}
	
	public HashMap getTwitterMessage() throws InterruptedException, ClassNotFoundException, SQLException {
		for (int i = 1; i < 11; i++) {
			genTweets(keyword, i);
			System.out.println(" number : " + i);
		}
		query.getSelectTwitter();
		return map;
	}
	
	public void genTweets(String keyword, int i) throws InterruptedException, ClassNotFoundException, SQLException {
		ArrayList array = new ArrayList();
        String requestUrl = "http://search.twitter.com/search.json?q=" + keyword + "%20API&result_type=recent&count=100&locale=en&page="+i + "&geocode=30.9,-99.85,268.82mi";
        String inputLine, result = "";
        String temp = "";
        
        try {
            URL url = new URL(requestUrl.toString());
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            
            while ((inputLine = in.readLine()) != null) {
                result = result.concat(inputLine);
            }
        } catch (Exception e) {
        	
        }
		
//		System.out.println("original text : " + temp);
		array = fromJSONObjectToString(result);
		
		for (int j = 0; j < array.size(); j++) {
			query.insertTwitter(keyword, (String) array.get(j));
			System.out.println(array.get(j));
		}
	}

	private ArrayList fromJSONObjectToString(String temp) throws InterruptedException {
//		System.out.println("inside of numbers : " + temp.indexOf('[')  + "  " + temp.indexOf("],\"results_per_page\""));
//		System.out.println("inside of [] : " + temp);
		temp = temp.substring(temp.indexOf('[') + 1 , temp.indexOf("],\"results_per_page\""));
		String twitter = temp;
		String time = "";
		String message = temp;
		int endIndex = 0;
		ArrayList result = new ArrayList();
		boolean hasMessage = true;
		for (int i = 0; i < 16; i++) {
			System.out.println("message " + i + "th");
			
			hasMessage = true;
//			System.out.println("*************************************************");
//			System.out.println( "11111111111 indexstart : " + temp.indexOf("\"text\":") + " endindex : " + endIndex);
//			System.out.println("*************************************************");
//			System.out.println("inside of numbers : " + temp.indexOf("\"created_at\":") + " | " + temp.indexOf("\"from_user\":")  + " | " + temp.length());
			
//			System.out.println("cut start:" + temp);
//			System.out.println("inside of numbers : " + temp.indexOf("\"created_at\":") + " | " + temp.indexOf("\"from_user\":")  + " | " + temp.length());

//			System.out.print("time = " + time + "|||");
//			System.out.println("compare : " + temp.indexOf(",\"to_user_id\":") + " and " + temp.indexOf(",\"to_user\":") );
			
			if (temp.indexOf(",\"to_user_id\":")== -1 && temp.indexOf(",\"to_user\":")== -1) hasMessage = false;
			
			if ( temp.indexOf(",\"to_user_id\":") != -1 && (temp.indexOf(",\"to_user\":") == -1 || temp.indexOf(",\"to_user_id\":") < temp.indexOf(",\"to_user\":"))) {
				endIndex = temp.indexOf(",\"to_user_id\":");
			} else { 
				endIndex = temp.indexOf(",\"to_user\":");
			}
			
			if (hasMessage) {
				time = temp.substring(temp.indexOf("\"created_at\":")+14 , temp.indexOf("\"from_user\":")-2);
				message = temp.substring(Integer.valueOf(temp.indexOf("\"text\":")) + 8 , endIndex - 1);
				result.add(message);
				temp = temp.substring(endIndex + 30, temp.length());
			}
			
		}
		return result;
	}
}
